package com.itheima.service.Impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.dto.LoginDto;
import com.itheima.mapper.MemberMapper;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    public static final String LOGIN_KEY = "登录验证码";
    @Override
    public String login(LoginDto loginDto) {
        //获取验证码
        String code = (String) redisTemplate.opsForValue().get(LOGIN_KEY+loginDto.getTelephone());
        int numberMember = 0;
        numberMember =memberMapper.Find(loginDto.getTelephone());
        if (code.equals(loginDto.validateCode)){
            if(numberMember==0){
               memberMapper.add(loginDto);
            }else
                return MessageConstant.LOGIN_SUCCESS;
        }else{
                return MessageConstant.VALIDATECODE_ERROR;
        }
        return "登录有问题";
    }
}
